<template>
  <div class="clean-detail">
    <div class="clean-detail__select">
      <div class="clean-detail__select-city">
        <el-select v-model="city" size="mini" @change="changeCity">
          <el-option
            v-for="(item, i) in cityList"
            :key="i"
            :label="item"
            :value="item"
            >
          </el-option>
        </el-select>
      </div>
      <div class="clean-detail__select-zones">
        <el-select v-model="zoneId" size="mini" @change="getData">
          <el-option
            v-for="item in zonesList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
            >
          </el-option>
        </el-select>
      </div>
      <div class="clean-detail__select-date" style="margin-right: 10px">
        <el-date-picker
          v-model="startTime"
          type="month"
          :clearable="false"
          size="mini"
          value-format="yyyy-MM"
          format="yyyy年M月"
          @change="getData">
        </el-date-picker>
      </div>-
      <div class="clean-detail__select-date" style="margin-left: 10px">
        <el-date-picker
          v-model="endTime"
          type="month"
          :clearable="false"
          size="mini"
          value-format="yyyy-MM"
          format="yyyy年M月"
          @change="getData">
        </el-date-picker>
      </div>
    </div>
    <div class="clean-detail__content">
      <ve-histogram ref="chart" :data="chartData" :settings="chartSettings" :extend="chartExtend" width="95%" height="15rem" :colors="colors2"></ve-histogram>
      <ve-bar :data="lineData" :settings="lineSettings" :extend="lineExtend" width="95%" height="15rem" :colors="colors"></ve-bar>
    </div>
  </div>
</template>
<script>
import { Select, Option, DatePicker } from 'element-ui'
import Histogram from 'v-charts/lib/histogram.common'
import Bar from 'v-charts/lib/bar.common'
import { getBjbaRound, getZonesList } from 'src/api/api'
import { formatDate, getDay, formatFontSize, getPreMonth } from 'js/utils'
import storage from 'good-storage'
export default {
  components: {
    [Select.name]: Select,
    [Option.name]: Option,
    [Histogram.name]: Histogram,
    [Bar.name]: Bar,
    [DatePicker.name]: DatePicker,
  },
  props: {
    service: {
      type: String,
      default: ''
    }
  },
  data(){
    // this.colors = ['rgba(10, 140, 255,0.7)', 'rgba(229, 111, 130,0.7)']
    this.colors = ['#634fda', '#E56F82']
    this.colors2 = ['#45F0EA', '#53ED73', '#0A8CFF', '#CAB617']
    return {
      startTime: formatDate('yyyy-mm', getPreMonth(5)),
      endTime: formatDate('yyyy-mm', new Date()),
      cityList: storage.get('cityList'),
      city: '杭州',
      zonesList: [],
      zoneId: 14,
      lineData: {
        columns: [],
        // columns: ['month', 'zkdks','bzBjNum'],
        // columns: ['month', 'xgdks','bzBaNum'],
        rows: []
      },
      lineSettings: {
        labelMap: {
          'zkdks': '实际打卡数',
          'bzBjNum': '标准打卡数',
          'xgdks': '实际打卡数',
          'bzBaNum': '标准打卡数'
        },
      },
      lineExtend: {
        legend: {
          show: true,
          textStyle: {
            color: '#fff',
            fontSize: formatFontSize(0.1),
          },
          selectedMode: false,
          right: '2%',
          top: '1%',
          orient: 'vertical',
          itemWidth: formatFontSize(0.15),
          itemHeight: formatFontSize(0.08),
        },
        xAxis: {
          axisLabel: {
            fontSize: formatFontSize(0.1),
            color: '#8EC7DC',
            formatter: param => {
              let ta = param/1000
              return ta + 'k'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#fff',
              opacity: 0.1,
            }
          },
        },
        yAxis: {
          axisLabel: {
            color: '#fff',
            fontSize: formatFontSize(0.1),
            // opacity: 0.5
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#fff',
              opacity: 0.1,
            }
          },
          // max: 5000,
          splitNumber: 5,
        },
        grid: {  
          left: '0',  
          right: '7%',
          top: '30%',
          bottom: '5%',  
        },
        series: {
          barWidth: formatFontSize(0.07),
          type: 'bar',
          label: { 
            show: false, 
            position: 'top',
            color: '#fff',
            fontSize: formatFontSize(0.1),
          },
        }
      },
      chartData: {
        columns: [],
        // columns: ['month', 'bjNum', 'bjzgNum', 'zkdwzs'],
        // columns: ['month', 'baNum', 'bazgNum', 'xgdws'],
        rows: []
      },
      chartSettings: {
        labelMap: {
          'bjNum': '平均总人数',
          'bjzgNum': '平均在岗人数',
          'zkdwzs': '平均巡更点位',
          'baNum': '平均总人数',
          'bazgNum': '平均在岗人数',
          'xgdws': '平均巡更点位',
        },
      },
      chartExtend: {
        legend: {
          show: true,
          textStyle: {
            color: '#fff',
            fontSize: formatFontSize(0.1),
            lineHeight: formatFontSize(0.05)
          },
          selectedMode: false,
          right: '0',
          top: '1%',
          orient: 'vertical',
          align: 'left',
          itemWidth: formatFontSize(0.15),
          itemHeight: formatFontSize(0.08),
        },
        xAxis: {
          axisLabel: {
            fontSize: formatFontSize(0.1),
            color: '#8EC7DC'
          },
        },
        yAxis: {
          axisLabel: {
            color: '#fff',
            fontSize: formatFontSize(0.1),
            // opacity: 0.5
          },
          axisLine: {
            show: true,
            lineStyle: {
              color: '#fff',
              opacity: 0.1,
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              color: '#fff',
              opacity: 0.1,
            }
          },
          // max: 5000,
          splitNumber: 5,
        },
        grid: {  
          left: '5%',  
          right: '0',
          top: '40%',
          bottom: '5%',  
        },
        series: {
          barWidth: formatFontSize(0.07),
          type: 'bar',
          label: { 
            show: false, 
            position: 'top',
            color: '#fff',
            fontSize: formatFontSize(0.1),
          },
        }
      },
    }
  },
  mounted() {
    this._getZonesList()
    this.getData()
  },
  methods: {
    _getZonesList() {
      let params = {
        city: this.city
      }
      getZonesList(params).then(res=> {
        if (res.success === true) {
          this.zonesList = res.data
        }
      })
    },
    getData() {
      let params = {
        city: this.city,
        zoneId: this.zoneId,
        startTime: this.startTime,
        endTime: this.endTime,
      }
      getBjbaRound(params).then(res=>{
        if (res.success === true) {
          this.chartData.rows = res.data
          this.lineData.rows = res.data
          if(this.service === 'bj') {
            this.lineData.columns = ['month', 'zkdks','bzBjNum']
            this.chartData.columns = ['month', 'bjNum', 'bjzgNum', 'zkdwzs']
          } else if (this.service === 'ba') {
            this.lineData.columns = ['month', 'xgdks','bzBaNum']
            this.chartData.columns = ['month', 'baNum', 'bazgNum', 'xgdws']
          }
        }
      })
    },
    changeCity() {
      let params = {
        city: this.city
      }
      getZonesList(params).then(res=> {
        if (res.success === true) {
          this.zonesList = res.data
          this.zoneId = res.data[0].id
          this.getData()
        }
      })
    }
  }
}
</script>
<style lang="scss">
.clean-detail {
  &__select {
    // text-align: right;
    padding: 5px 10px 5px 0;
    font-size: 14px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    color: #AAECFF;
    .el-select, .el-date-editor {
      .el-input__inner {
        // width: 150px;
        border: 0;
        border-radius: 0;
        background:rgba(11,21,58,1);
        box-shadow:1px 1px 0px 0px rgba(255,255,255,0.25), 0px 2px 4px 0px rgba(0, 0, 0, 1);
        color: #AAECFF;
        font-size: 14px;
      }
      .el-input__icon {
        color: #AAECFF;
        font-size: 14px;
      }
    }
    &-city {
      margin-right: 10px;
      .el-select {
        width: 80px;
      }
    }
    &-zones {
      margin-right: 10px;
      .el-select {
        width: 120px;
      }
    }
    &-date {
      // margin-right: 20px;
      .el-date-editor {
        width: 90px;
      }
      .el-input__prefix ,.el-input__suffix{
        display: none;
      }
      .el-input--prefix .el-input__inner {
        padding-left: 5px;
      }
      .el-input--suffix .el-input__inner {
          padding-right: 0px;
      }
    }
  }
  &__content {
    display: flex;
    flex-wrap: nowrap;
  }
}
</style>